package com.elm.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.elm.model.Admin;
import com.elm.model.Seller;
import com.elm.model.User;

import com.elm.service.SellerService;
import com.elm.service.UserService;

import com.elm.serviceimpl.SellerServiceImpl;
import com.elm.serviceimpl.UserServiceImpl;

public class UserLoginController extends HttpServlet{
	UserService us = new UserServiceImpl();
	SellerService ss = new SellerServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		PrintWriter pw = resp.getWriter();
		HttpSession session = req.getSession();
		String username = req.getParameter("username");
		String role = req.getParameter("role");
		String userpsw = req.getParameter("userpsw");
		if("用户".equals(role)){
			User u = new User(username, userpsw);
			User user = new User();
			user = us.userLogin(u);
			if(user.getUsername()!=null){
				session.setAttribute("currentUser", user);
				pw.write("1");
				pw.close();
			}else{
				pw.write("0");
				pw.close();
			}
		}
		if("商家".equals(role)){
			Seller s = new Seller(username, userpsw);
			Seller seller = new Seller();
			seller = ss.sellerLogin(s);
			if(seller.getSellername()!=null){
				session.setAttribute("currentSeller", seller);
				pw.write("2");
				pw.close();
			}
			else{
				pw.write("0");
				pw.close();
			}
		}
		
	}
}
